#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRegularExpression>
#include <QDateTime>
#include <QMessageBox>
#include <QJsonDocument>
#include <QJsonObject>
#include <QValueAxis>
#include <csignal>  // 添加这个头文件
#include <QNetworkInterface>  // 网络接口相关
#include <QHostAddress>       // IP地址处理
#include <QDebug>

void MainWindow::setupPowerControlUI()
{
    // 创建电源控制工具按钮
    powerToolButton = new QToolButton(this);
    powerToolButton->setText(tr("Power Control"));
    powerToolButton->setPopupMode(QToolButton::InstantPopup);
    powerToolButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
    powerToolButton->setStyleSheet("QToolButton { padding: 5px; }");

    // 创建主菜单
    powerMenu = new QMenu(this);
    powerMenu->setStyleSheet(
        "QMenu { background-color: #f0f0f0; border: 1px solid #ccc; }"
        "QMenu::item { padding: 5px 20px 5px 10px; }"
        "QMenu::item:selected { background-color: #d0d0d0; }"
        );

    // 1. 相机电源子菜单
    QMenu* cameraMenu = createPowerSubMenu("Camera Power", ":/icons/camera.png");
    connect(cameraMenu, &QMenu::aboutToShow, [this]() {
        updatePowerButtonStates(btnCameraPowerOn, btnCameraPowerOff, cameraState.status != -1);
    });

    // 2. 加热电源子菜单
    QMenu* heatMenu = createPowerSubMenu("Heat Power", ":/icons/heat.png");
    connect(heatMenu, &QMenu::aboutToShow, [this]() {
        updatePowerButtonStates(btnHeatPowerOn, btnHeatPowerOff, hwStatus.isCooling);
    });

    // 3. 真空电源子菜单
    QMenu* vaccuMenu = createPowerSubMenu("Vaccu Power", ":/icons/vacuum.png");
    //connect(vaccuMenu, &QMenu::aboutToShow, [this]() {
        //updatePowerButtonStates(btnVaccuPowerOn, btnVaccuPowerOff, hwStatus.vacuumPumpRunning);
    //});

    // 添加到主菜单
    powerMenu->addMenu(cameraMenu);
    powerMenu->addMenu(heatMenu);
    powerMenu->addMenu(vaccuMenu);

    // 设置工具按钮的菜单
    powerToolButton->setMenu(powerMenu);

    // 添加到工具栏
    ui->mainToolBar->addWidget(powerToolButton);
}


QMenu* MainWindow::createPowerSubMenu(const QString& title, const QString& iconPath)
{
    QMenu* subMenu = new QMenu(title, this);

    // 设置子菜单样式
    subMenu->setStyleSheet(
        "QMenu { background-color: #f8f8f8; border: 1px solid #ddd; }"
        "QMenu::item { padding: 5px 15px; }"
        "QMenu::item:selected { background-color: #e0e0e0; }"
        );

    // 添加图标
    if (QFile::exists(iconPath)) {
        subMenu->setIcon(QIcon(iconPath));
    }

    // 创建按钮容器
    QWidget* buttonWidget = new QWidget();
    QHBoxLayout* buttonLayout = new QHBoxLayout(buttonWidget);
    buttonLayout->setContentsMargins(5, 2, 5, 2);
    buttonLayout->setSpacing(5);

    // 创建On/Off按钮
    QPushButton* onButton = new QPushButton("On", buttonWidget);
    QPushButton* offButton = new QPushButton("Off", buttonWidget);

    // 设置按钮样式
    QString buttonStyle =
        "QPushButton {"
        "   padding: 3px 8px;"
        "   border: 1px solid #aaa;"
        "   border-radius: 3px;"
        "}"
        "QPushButton:hover {"
        "   background-color: #e0e0e0;"
        "}"
        "QPushButton:checked {"
        "   background-color: #4CAF50;"
        "   color: white;"
        "}"
        "QPushButton:checked:hover {"
        "   background-color: #45a049;"
        "}";

    onButton->setStyleSheet(buttonStyle);
    offButton->setStyleSheet(buttonStyle);

    // 添加到布局
    buttonLayout->addWidget(onButton);
    buttonLayout->addWidget(offButton);

    // 创建WidgetAction
    QWidgetAction* buttonAction = new QWidgetAction(subMenu);
    buttonAction->setDefaultWidget(buttonWidget);

    // 添加到子菜单
    subMenu->addAction(buttonAction);
    // 添加悬停效果
    subMenu->setAttribute(Qt::WA_TranslucentBackground);
    subMenu->setWindowFlags(subMenu->windowFlags() | Qt::FramelessWindowHint);
    subMenu->setAttribute(Qt::WA_TranslucentBackground);

    // 添加动画效果
    QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(subMenu);
    subMenu->setGraphicsEffect(opacityEffect);
    QPropertyAnimation* animation = new QPropertyAnimation(opacityEffect, "opacity");
    animation->setDuration(150);
    animation->setStartValue(0);
    animation->setEndValue(1);
    animation->start();

    // 返回按钮指针
    if (title.contains("Camera")) {
        btnCameraPowerOn = onButton;
        btnCameraPowerOff = offButton;
    }
    else if (title.contains("Heat")) {
        btnHeatPowerOn = onButton;
        btnHeatPowerOff = offButton;
    }
    else if (title.contains("Vaccu")) {
        btnVaccuPowerOn = onButton;
        btnVaccuPowerOff = offButton;
    }

    return subMenu;
}



void MainWindow::updatePowerButtonStates(QPushButton* onBtn, QPushButton* offBtn, bool isOn)
{
    // 更新按钮选中状态
    onBtn->setChecked(isOn);
    offBtn->setChecked(!isOn);

    // 更新按钮样式
    QString onStyle = onBtn->styleSheet();
    QString offStyle = offBtn->styleSheet();

    if (isOn) {
        onStyle += "background-color: #4CAF50; color: white;";
        offStyle.replace("background-color: #4CAF50;", "");
    } else {
        offStyle += "background-color: #f44336; color: white;";
        onStyle.replace("background-color: #f44336;", "");
    }

    onBtn->setStyleSheet(onStyle);
    offBtn->setStyleSheet(offStyle);
}

